package sk.pitonak.mhd;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;


public class OnlineResult extends Result {
	private static final int FLAG_BOLD = 1;
	private static final int FLAG_PADDING = 2;
	
	private String[] mLines;
	private int[] mTypeface;

	public OnlineResult(Element table) {
		Elements rows = table.select("tr.theader2, tr.theader2 + tr > td > table, tr.theader2 + tr div[id~=sumar]");
		mLines = new String[rows.size()];
		mTypeface = new int[rows.size()];
		
		int i = 0;
		for(Element row : rows) {
			mLines[i] = row.text();
			
			mTypeface[i] = 0;
			if(row.hasClass("theader2")) {
				mTypeface[i] |= FLAG_BOLD;
				if(i != 0 && row.text().matches("^[0-9]+.*")) {
					mTypeface[i] |= FLAG_PADDING;
					Log.e("", "matches");
				}
			}
			
			Log.w("", row.text());
			++i;
		}
	}

	@Override
	public View getView(Context context) {
		LinearLayout layout = new LinearLayout(context);
		layout.setOrientation(LinearLayout.VERTICAL);
		
		int i = 0;
		for(String line : mLines) {
			TextView tv = new TextView(context);
			tv.setText(line);
			
			if((mTypeface[i] & FLAG_BOLD) > 0) tv.setTypeface(null, Typeface.BOLD);
			if((mTypeface[i] & FLAG_PADDING) > 0) tv.setPadding(0, 20, 0, 0);
			
			layout.addView(tv, new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT
			));
			++i;
		}
		
		return layout;
	}
}
